﻿using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Udp;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AnesSystem.Controls.CommonTables.SC.DCYY
{
    /// <summary>
    /// ShuQiangFangShiCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class ShuQiangFangShiCtrl : CustomCommonTableCtrl
    {
        private DispatcherTimer timer;
        private int i = 0;
        public ShuQiangFangShiCtrl()
        {
            InitializeComponent();
            GetShuQianZqJiZheng();
            InitalTimer();
            this.Loaded += ShuQiangFangShiCtrl_Loaded;
            IniteRadioBation();          
        }

        private void ShuQiangFangShiCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            FunctionListHeaderCtrlViewModel.Instance.DisplayButton(true, FunctionListButton.打印, FunctionListButton.刷新, FunctionListButton.检验数据);
            DataManager.Instance.IsNotNeedSaveData = false;
        }

        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += Timer_Tick;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (i < 30)
            {
                PatientLisDataModel p = Util.ExtensionMethods.GetLisData();
                if (p != null && p.Result == true && p.Zyh.Equals(DataManager.Instance.CurrentPatientLink.Patient.Zyh))
                {
                    FillLisData(p);
                    MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                    i = 0;
                    timer.Stop();
                    return;
                }
                i++;
            }
            else
            {
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                i = 0;
                timer.Stop();
                MessageBoxCommon.ShowDialog("获取数据结束，若未获取到检验数据，请稍等几分钟重试！！！", MessageBoxCommon.OK);
            }
        }

        public override void GetLisData()
        {
            string PatId = DataManager.Instance.CurrentPatientLink.Patient.PatId;
            if (string.IsNullOrEmpty(PatId))
            {
                MessageBoxCommon.ShowDialog("此病人无病人ID；无法获取检验数据。。。", MessageBoxCommon.OK);
                return;
            }
            try
            {
                Task.Factory.StartNew(delegate
                {

                    if (Util.ExtensionMethods.GetLisData() != null)
                    {
                        PatientLisDataModel p = Util.ExtensionMethods.GetLisData();
                        Dispatcher.Invoke(delegate
                        {
                            FillLisData(p);
                            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
                        });
                        return;
                        //p.Result = false;
                        //PatientLisDataService.Instance.AddUpdate(p);
                    }
                    UdpModel udpModel = new UdpModel();
                    udpModel.PatientId = DataManager.Instance.CurrentPatientLink.Id.ToString();
                    udpModel.PatId = PatId ;
                    udpModel.Zycs = DataManager.Instance.CurrentPatientLink.Patient.Zycs;
                    udpModel.Zyh = DataManager.Instance.CurrentPatientLink.Patient.Zyh;
                    udpModel.UpdateDate = DateTime.Now.ToString();
                    udpModel.AccountID = DataManager.Instance.CurrentPatientLink.Patient.Brxm;
                    UdpMangager.Instance.SendUdpServerMsg(ServiceSign.GETLISDATA, udpModel);
                    timer.Start();//执行查询方法的线程  
                });
                MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }
        private void FillLisData(PatientLisDataModel p)
        {
            Dictionary<string, object> jianYanMap = Util.ExtensionMethods.GetJianYanMap(p.JianYan);
            DataManager.Instance.FillCtrlValue(jianYanMap);
            DataManager.Instance.FillAndSaveXMLTableValue(jianYanMap);
        }

        private void rq_GotFocus(object sender, RoutedEventArgs e)
        {
            CustomTextCtrl custext = sender as CustomTextCtrl;
            CusDatePickerWin cusDatePicker = new CusDatePickerWin();
            cusDatePicker.SelectedDateTime = DateTime.Now;

            cusDatePicker.ShowDialog();

            if (cusDatePicker.Result)
            {
                rq.Value = cusDatePicker.SelectedDateTime.ToString(DateFormat.DATE_FORMAT_STR_YMD_CN);//2005年11月5日
            }
        }
        private void IniteRadioBation()
        {
            new RadioButtonHelper().ChangeRadioSatus(mz_y, mz_n);
            new RadioButtonHelper().ChangeRadioSatus(qx,ss,hm);
            new RadioButtonHelper().ChangeRadioSatus(fb_y, fb_n);
            new RadioButtonHelper().ChangeRadioSatus(zg_y, zg_n);
            new RadioButtonHelper().ChangeRadioSatus(qd_y, qd_n);
            new RadioButtonHelper().ChangeRadioSatus(xgn1,xgn2,xgn3,xgn4);
            new RadioButtonHelper().ChangeRadioSatus(fgn1,fgn2,fgn3,fgn4);
            new RadioButtonHelper().ChangeRadioSatus(asa1,asa2,asa3,asa4,asa5);
            new RadioButtonHelper().ChangeRadioSatus(bw_y, bw_n);
            new RadioButtonHelper().ChangeRadioSatus(zq, jz);

        }
        private  void GetShuQianZqJiZheng()
        {
          if( DataManager.Instance.CurrentPatientLink.Application.Type.Equals(Constants.OR_TYPE_URGENT)|| DataManager.Instance.CurrentPatientLink.Application.Type == null)
            {
                jz.Value = true;
            }
            else
            {
                zq.Value = true;
            }
            
        }
    }
}
